//
//  CourserRecommendCell.m
//  ShowYork
//
//  Created by hxh on 2020/8/28.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonRecommendCell.h"
#import "LessonClassItemCell.h"

#define itemRate 190.0/165.0f
#define itemW (kScreenW - 48)/2
#define itemH itemW * itemRate

@interface LessonRecommendCell ()<UICollectionViewDelegate,UICollectionViewDataSource,LessonClassItemCellDelegate,LessonRecommendCellDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,copy)NSArray *sourceArray;
@end

@implementation LessonRecommendCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(itemW,itemH);
    layout.minimumInteritemSpacing = 8;
    layout.sectionInset = UIEdgeInsetsMake(16,16, 16,16);
    layout.minimumLineSpacing = 16.0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[LessonClassItemCell class] forCellWithReuseIdentifier:@"LessonClassItemCell"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.contentView addSubview:self.collectionView];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(0).priorityHigh();
    }];
}

- (void)setRecommendSource:(NSArray<LessonPageItemModel *> *)sourceArr{
        
    self.sourceArray = sourceArr;
    
    NSInteger max = MIN(6,self.sourceArray.count);
    NSInteger maxCount = max/2 + max%2;
    
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(maxCount *itemH + (maxCount + 1) * 16).priorityHigh();
    }];
    
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  MIN(self.sourceArray.count,6);
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LessonClassItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LessonClassItemCell" forIndexPath:indexPath];
    [cell setModel:self.sourceArray[indexPath.item]];
    cell.delegate = self;
    return cell;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"LessonRecommendCell";
    LessonRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[LessonRecommendCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
#pragma mark - CourseClassItemCellDelegate
-(void)addCoursePlanAtCurrentModel:(LessonPageItemModel *)model{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addorRemoveCourseRecommendPlanAtCurrentModel:)]) {
        [self.delegate addorRemoveCourseRecommendPlanAtCurrentModel:model];
    }
}
- (void)showCourseDetailWithCurrentModel:(LessonPageItemModel *)model{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didCourseDetailAtCurrentModel:)]) {
        [self.delegate didCourseDetailAtCurrentModel:model];
    }
}
@end
